#!/bin/bash

STANDALONE_PATH=/home/wzl/standalone

# using release mode
WORK_DIR=$STANDALONE_PATH/out/x64.release
ES2ABC=$WORK_DIR/arkcompiler/ets_frontend/es2abc
ARK_JS_VM=$WORK_DIR/arkcompiler/ets_runtime/ark_js_vm

TEST_CASE=$1
TEST_CASE_BASENAME=${TEST_CASE%.*}

rm -f output*.txt
rm -f expect.txt
rm -f interp.txt
rm -f baseline.txt

# set env
echo export LD_LIBRARY_PATH=$WORK_DIR/thirdparty/bounds_checking_function:$WORK_DIR/arkcompiler/ets_runtime
export LD_LIBRARY_PATH=$WORK_DIR/thirdparty/bounds_checking_function:$WORK_DIR/arkcompiler/ets_runtime

# generate bytecode
echo $ES2ABC --merge-abc --output $TEST_CASE_BASENAME.abc $TEST_CASE
$ES2ABC --merge-abc --output $TEST_CASE_BASENAME.abc $TEST_CASE

# execute
echo "$WORK_DIR/arkcompiler/ets_runtime/ark_js_vm --asm-interpreter=true --entry-point=$TEST_CASE_BASENAME  --test-assert=true  $TEST_CASE_BASENAME.abc"
$WORK_DIR/arkcompiler/ets_runtime/ark_js_vm --asm-interpreter=true --entry-point=$TEST_CASE_BASENAME  --test-assert=true  $TEST_CASE_BASENAME.abc >>interp.txt 2>&1

echo "$WORK_DIR/arkcompiler/ets_runtime/ark_js_vm --asm-interpreter=true --entry-point=$TEST_CASE_BASENAME --compiler-enable-baselinejit=true --compiler-force-baselinejit-compile-main=true --test-assert=true  $TEST_CASE_BASENAME.abc"
$WORK_DIR/arkcompiler/ets_runtime/ark_js_vm --asm-interpreter=true --entry-point=$TEST_CASE_BASENAME --compiler-enable-baselinejit=true --compiler-force-baselinejit-compile-main=true --test-assert=true  $TEST_CASE_BASENAME.abc >>baseline.txt 2>&1

# verify result
if [ $? -ne 0 ]
then
    echo "run error, fail case: $1"
    exit -1
fi

if [ -e expect_output.txt ]
then
    grep -v "^#" expect_output.txt | grep -v "^$" > expect.txt

    diff interp.txt baseline.txt
    if [ $? -ne 0 ]
    then
        echo "having different result, fail case: $1"
        exit -2
    else
        echo "pass case: $1"
        exit 0
    fi
else
    echo "pass case: $1"
    exit 0
fi
